// 练习

use std::str::FromStr;
use std::num::ParseIntError;
use std::fmt;

#[derive(Debug, PartialEq)]
struct Bed {
    chromosome: String,
    start: i64,
    end: i64,
    name: String,
    id: String,
    strand: String
}

impl FromStr for Bed {
    type Err = ParseIntError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let coords: Vec<&str> = s.trim_matches(|p| p == ' ' || p == '\t' || p == '\n' )
                                 .split('\t')
                                 .collect();

        let chromosome = coords[0].to_owned();
        let start = coords[1].parse::<i64>()?;
        let end = coords[2].parse::<i64>()?;
        let name = coords[3].to_owned();
        let id = coords[4].to_owned();
        let strand = coords[5].to_owned();

        Ok(Bed { chromosome, start, end, name, id, strand })
    }
}

impl fmt::Display for Bed {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}\t{}\t{}\t{}\t{}\t{}", self.chromosome, self.start, self.end, self.name, self.id, self.strand)
    }
}

fn main() {
    let p = Bed::from_str("1\t100\t200\tname\tid\t+").unwrap();

    println!("DEBUG: {:?}", p);
    println!("Just format: {}; to_string(): {}", p, p.to_string());
}
